package com.elong.android.specialhouse.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.ApartmentListResponse;
import com.elong.android.specialhouse.entity.HouseItemList;
import com.elong.android.specialhouse.utils.ImageLoaderUtils;
import com.elong.android.specialhouse.utils.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ApartmentListAdapter extends BaseAdapter{

	private Context mContext;
	private ApartmentListResponse mSearchResponse;
	private OnCollectionListener mlistener ;
	
	// 图片异步加载工具
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private DisplayImageOptions headeOptions;

	public ApartmentListAdapter(Context context, ApartmentListResponse searchResult) {
		this.mContext = context;
		this.mSearchResponse = searchResult;
		this.options = ImageLoaderUtils.getHouseImageOptions();
		this.headeOptions = ImageLoaderUtils.getHeaderImageOptions();
	}

	@Override
	public int getCount() {
		return mSearchResponse.HouseItemList.size();
	}

	@Override
	public HouseItemList getItem(int position) {
		return mSearchResponse.HouseItemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.apartment_list_item, parent, false);
			bindViewHolder(convertView);
		}
		onListItemPopulate(convertView, position);
		return convertView;
	}

	private void onListItemPopulate(View v, final int position) {
		ViewHolder holder = (ViewHolder) v.getTag();
		holder.resetView();
		final HouseItemList item = this.mSearchResponse.HouseItemList.get(position);
		
		if(item.DailyPrice!=0){
			holder.tvPrice.setVisibility(View.VISIBLE);
	        int price = StringUtils.format(item.DailyPrice);
	        SpannableString priceSpan = new SpannableString("￥" + price + "/晚");
	        int priceTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 22, mContext.getResources().getDisplayMetrics());
	        priceSpan.setSpan(new AbsoluteSizeSpan(priceTextSize, false), 1, priceSpan.length() - 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
	        priceSpan.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.theme_yellow)), 1, priceSpan.length() - 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
	        holder.tvPrice.setText(priceSpan);
		}
		if(!TextUtils.isEmpty(item.HouseName)){
			holder.tvHouseName.setVisibility(View.VISIBLE);
			holder.tvHouseName.setText(item.HouseName);
		}
		if(!TextUtils.isEmpty(item.BusinessAreaName) || !TextUtils.isEmpty(item.DistrictName)){
			holder.tvAddress.setVisibility(View.VISIBLE);
			holder.tvAddress.setText(TextUtils.isEmpty(item.BusinessAreaName) ? TextUtils.isEmpty(item.DistrictName)?"":item.DistrictName : item.BusinessAreaName);
		}
		if(item.RentalType==0){
			holder.tvHouseType.setText(item.RoomAndLobby);
		}else if(item.RentalType==1){
			holder.tvHouseType.setText(R.string.house_type_room);
		}else if(item.RentalType==2){
			holder.tvHouseType.setText(R.string.house_type_bed);
		}else {
			holder.tvHouseType.setVisibility(View.GONE);
		}
		
		imageLoader.displayImage(item.RoomPicUrl, holder.ivHousePhoto, options);
		
		imageLoader.displayImage(item.LandlordPicUrl, holder.ivHeader,headeOptions);
		if(item.IsCollected){
			holder.ivFavlist.setImageResource(R.drawable.icon_collect_selected);
		}else {
			holder.ivFavlist.setImageResource(R.drawable.icon_collect_unselected);
		}
		holder.ivFavlist.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mlistener != null) {
					mlistener.onCollectinClicked(item, position);
				}
			}
		});
		if(item.Discount < 1){
			holder.llDiscount.setVisibility(View.VISIBLE);
			int price = StringUtils.format(item.DiscountPrice);
			SpannableString priceSpan = new SpannableString("￥" + price + "/晚");
			int priceTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, mContext.getResources().getDisplayMetrics());
			priceSpan.setSpan(new AbsoluteSizeSpan(priceTextSize, false), 1, priceSpan.length() - 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
			priceSpan.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.theme_red)), 1, priceSpan.length() - 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
			holder.tvDiscountPrice.setText(priceSpan);
		}else{
			holder.llDiscount.setVisibility(View.GONE);
		}
	}

	public void setOnCollectionClickListener(OnCollectionListener listener) {
		this.mlistener = listener ;
	}

	private void bindViewHolder(View convertView) {
		ViewHolder holder = new ViewHolder();
		holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
		holder.tvHouseName = (TextView) convertView.findViewById(R.id.tv_house_name);
		holder.tvAddress = (TextView) convertView.findViewById(R.id.tv_address);
		holder.tvHouseType = (TextView) convertView.findViewById(R.id.tv_house_type);
		holder.ivFavlist = (ImageView) convertView.findViewById(R.id.iv_favlist);
		holder.ivHeader = (ImageView) convertView.findViewById(R.id.iv_header);
		holder.ivHousePhoto = (ImageView) convertView.findViewById(R.id.iv_house_photo);
		holder.llDiscount = (LinearLayout) convertView.findViewById(R.id.ll_discount);
		holder.tvDiscountPrice = (TextView) convertView.findViewById(R.id.tv_discount_price);
		convertView.setTag(holder);
	}
	private class ViewHolder {
		public TextView tvPrice;
		public ImageView ivFavlist;
		public ImageView ivHeader;
		public ImageView ivHousePhoto;
		public TextView tvHouseName;
		public TextView tvAddress;
		public TextView tvHouseType;
		public LinearLayout llDiscount;
		public TextView tvDiscountPrice;
		public void resetView() {
			tvPrice.setVisibility(View.GONE);	
			tvHouseName.setVisibility(View.GONE);
			tvAddress.setVisibility(View.GONE);
			llDiscount.setVisibility(View.GONE);
		}
	}

	public interface OnCollectionListener {
		void onCollectinClicked(HouseItemList item, int position) ;
	}
	
}
